//
//  SIMBaseSearchBar.m
//  SecurityInformationManagement
//
//  Created by Hayder on 2020/12/5.
//  Copyright © 2020 codeDog. All rights reserved.
//

#import "HHBaseSearchBar.h"
#import "HHGlobalDefine.h"
@implementation HHBaseSearchBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = kGray_240;
        
        CGFloat margin = 15;
        self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.searchBtn.frame = CGRectMake(HH_SCREEN_WIDTH-margin-70, 10, 70, 40);
        [self.searchBtn setBackgroundColor:kThemeColor];
        [self.searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        self.searchBtn.titleLabel.font = [UIFont systemFontOfSize:15.f];
        self.searchBtn.layer.cornerRadius = 5;
        self.searchBtn.layer.masksToBounds = YES;
        [self.searchBtn addTarget:self action:@selector(didClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.searchBtn];
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(margin, 10, HH_SCREEN_WIDTH - 15*3 - self.searchBtn.width, 40)];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.cornerRadius = 5;
        self.bgView.layer.masksToBounds = YES;
        [self addSubview:self.bgView];
        
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(margin, 5, self.bgView.width-2*10, 30)];
        self.textField.font = [UIFont systemFontOfSize:15.f];
        self.textField.borderStyle = UITextBorderStyleNone;
        [self.textField addTarget:self action:@selector(textEditing:) forControlEvents:UIControlEventEditingChanged];
        self.textField.clearButtonMode = UITextFieldViewModeAlways;
        [self.bgView addSubview:self.textField];
        
    }
    
    return self;
}

- (void)didClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(didClickSearchBtn:)]) {
        [self.delegate didClickSearchBtn:self.textField.text];
    }
}

- (void)textEditing:(UITextField *)textField
{
    if ([self.delegate respondsToSelector:@selector(seachBarDidEditing:)]) {
        [self.delegate seachBarDidEditing:self.textField.text];
    }
}

@end
